package
{
	import com.demonsters.debugger.MonsterDebugger;
	import com.zero.zevent.LoaderEvent;
	import com.zero.zloader.DataLoader;
	import com.zero.zsingleton.ComponentSingleton;
	import com.zero.zsingleton.LanguageSingleton;
	import com.zero.zui.ExtendTextField;
	import com.zero.zui.ShapeTextButton;
	import com.zero.zutils.UiUtils;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import yzhkof.debug.DebugSystem;
	
	[SWF(width="500",height="500",frameRate="24",backgroundColor="#FFFFFF")]
	public class Main extends Sprite
	{
		private var _xmlLoader				:DataLoader;
		private var _languageLoader		:DataLoader;
		
		private var _tetris				:Tetris;
		
		private var _backgroundShape		:Shape;
		private var _stopBtn				:ShapeTextButton;
		private var _resumeBtn				:ShapeTextButton;
		private var _resetBtn				:ShapeTextButton;
		private var _scoreTxt				:ExtendTextField;
		private var _levelTxt				:ExtendTextField;

		public function Main()
		{
			super();
			initStage();
			initLoader();
		}
		
		private function initStage():void
		{
			stage.scaleMode 	= StageScaleMode.NO_SCALE;
			stage.align 		= StageAlign.TOP_LEFT;
			stage.stageWidth 	= TileUtils.STAGE_WIDTH;
			stage.stageHeight 	= TileUtils.STAGE_HEIGHT;
			DebugSystem.init(stage);
			MonsterDebugger.initialize(stage);
			
			this.x = TileUtils.GAME_X;
			this.y = TileUtils.GAME_Y;
		}
		
		private function initLoader():void
		{
			_languageLoader = new DataLoader(TilePath.LANGUGAGE_PATH);
			_languageLoader.addEventListener(LoaderEvent.COMPLETE, __languageLoadComplete);
			_languageLoader.loadFromWeb();
			
			_xmlLoader = new DataLoader(TilePath.XML_PATH);
			_xmlLoader.addEventListener(LoaderEvent.COMPLETE,  __xmlLoadComplete);
			_xmlLoader.loadFromWeb();
		}
		
		private function __languageLoadComplete(e:LoaderEvent):void
		{
			_languageLoader.removeEventListener(LoaderEvent.COMPLETE, __languageLoadComplete);
			LanguageSingleton.instance.analyze(e.currentTarget.content);
		}
		
		private function __xmlLoadComplete(e:LoaderEvent):void
		{
			_xmlLoader.removeEventListener(LoaderEvent.COMPLETE, __xmlLoadComplete);

			var _xml:XML = new XML(e.currentTarget.content);
			ComponentSingleton.instance.model.addComponentSet(_xml);
			
			initView();
			addEvent();
		}
		
		private function addEvent():void
		{
			addEventListener(Event.ENTER_FRAME, __enterFrameHandler);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, __onKeyDownHandler);
			
			_stopBtn.addEventListener(MouseEvent.CLICK, __onStopGameHandler);
			_resetBtn.addEventListener(MouseEvent.CLICK, __onResetGameHandler);
			_resumeBtn.addEventListener(MouseEvent.CLICK, __onResumeGameHandler);
		}
		
		private function __onStopGameHandler(e:MouseEvent):void
		{
			_tetris.stopGame();
		}
		
		private function __onResetGameHandler(e:MouseEvent):void
		{
			_tetris.resetGame();
		}
		
		private function __onResumeGameHandler(e:MouseEvent):void
		{
			_tetris.resumeGame();
		}
		
		private function removeEvent():void
		{
			removeEventListener(Event.ENTER_FRAME, __enterFrameHandler);
			stage.removeEventListener(KeyboardEvent.KEY_DOWN, __onKeyDownHandler);
		}
		
		private function __enterFrameHandler(e:Event):void
		{
			_tetris.updateTileShape();
			
			_scoreTxt.text	= LanguageSingleton.instance.GetTranslation("Main.scoreTxt.text", _tetris.gameScore);
			_levelTxt.text	= LanguageSingleton.instance.GetTranslation("Main.levelTxt.text", _tetris.gameLevel);
		}
		
		private function __onKeyDownHandler(e:KeyboardEvent):void
		{
			_tetris.tileStateChange(e.keyCode);
		}
		
		private function initView():void
		{
			_tetris 		= new Tetris();
			
			_stopBtn 	= ComponentSingleton.instance.createComponentByStylename("tetris.stopGameBtn");
			_resetBtn	= ComponentSingleton.instance.createComponentByStylename("tetris.ResetGameBtn");
			_resumeBtn	= ComponentSingleton.instance.createComponentByStylename("tetris.ResumeGameBtn");
			_scoreTxt	= ComponentSingleton.instance.createComponentByStylename("tetris.scoreText");
			_levelTxt	= ComponentSingleton.instance.createComponentByStylename("tetris.levelText");
			
			_stopBtn.text 	= LanguageSingleton.instance.GetTranslation("TileDisplayView.stopBtn.text");
			_resetBtn.text 	= LanguageSingleton.instance.GetTranslation("TileDisplayView.resetBtn.text");
			_resumeBtn.text = LanguageSingleton.instance.GetTranslation("TileDisplayView.resumeBtn.text");
			
			UiUtils.addToDisplay(_tetris, this);
			UiUtils.addToDisplay(_stopBtn, this);
			UiUtils.addToDisplay(_resetBtn, this);
			UiUtils.addToDisplay(_resumeBtn, this);
			UiUtils.addToDisplay(_scoreTxt, this);
			UiUtils.addToDisplay(_levelTxt, this);
		}
	}
}